package net.sourceforge.peers.sip.transaction;

import net.sourceforge.peers.Logger;


public class InviteClientTransactionStateCalling extends InviteClientTransactionState {

    public InviteClientTransactionStateCalling(String id,
                                               InviteClientTransaction inviteClientTransaction, Logger logger) {
        super(id, inviteClientTransaction, logger);
    }

    @Override
    public void timerAFires() {
        InviteClientTransactionState nextState = inviteClientTransaction.CALLING;
        inviteClientTransaction.setState(nextState);
        //当计时器 A 触发时，客户端事务必须将请求重传到传输层，并且必须将计时器的值重置为 2＊T1
        inviteClientTransaction.sendRetrans();
    }

    @Override
    public void timerBFires() {
        timerBFiresOrTransportError();
    }

    @Override
    public void transportError() {
        timerBFiresOrTransportError();
    }

    private void timerBFiresOrTransportError() {
        InviteClientTransactionState nextState = inviteClientTransaction.TERMINATED;
        inviteClientTransaction.setState(nextState);
        inviteClientTransaction.transactionUser.transactionTimeout(inviteClientTransaction);
    }

    @Override
    public void received2xx() {
        InviteClientTransactionState nextState = inviteClientTransaction.TERMINATED;
        inviteClientTransaction.setState(nextState);
        inviteClientTransaction.transactionUser.successResponseReceived(
                inviteClientTransaction.getLastResponse(), inviteClientTransaction);
    }

    @Override
    public void received1xx() {
        InviteClientTransactionState nextState = inviteClientTransaction.PROCEEDING;
        inviteClientTransaction.setState(nextState);
        inviteClientTransaction.transactionUser.provResponseReceived(
                inviteClientTransaction.getLastResponse(), inviteClientTransaction);
    }

    @Override
    public void received300To699() {
        InviteClientTransactionState nextState = inviteClientTransaction.COMPLETED;
        inviteClientTransaction.setState(nextState);
        inviteClientTransaction.createAndSendAck();
        inviteClientTransaction.transactionUser.errResponseReceived(inviteClientTransaction.getLastResponse());
    }


}
